


//
//  BELoginAndSignUpController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/9/2.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit


class BELoginAndSignUpController: UIViewController {
    
    //    private var ChooseToolBar: KKChooseToolBar = KKChooseToolBar()

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        automaticallyAdjustsScrollViewInsets = false
        //注册通知 ---> push忘记密码控制器的通知
        NotificationCenter.default.addObserver(self, selector: #selector(BELoginAndSignUpController.pushForgetPWDController), name: NSNotification.Name(rawValue: PushForgetPWDController), object: nil)
        
        // ---> dismisslogin控制器的通知
        NotificationCenter.default.addObserver(self, selector: #selector(BELoginAndSignUpController.dismissLoginController), name: NSNotification.Name(rawValue: DismissLoginController), object: nil)
        
//        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测试帐号", target: self, action: #selector(BELoginAndSignUpController.navRightBtnClick))
        
        
        view.backgroundColor = UIColor.white
        
        //        view.addSubview(ChooseToolBar)
        view.addSubview(collectionView)
        //        ChooseToolBar.snp_makeConstraints { (make) in
        //            make.top.equalTo(self.view).offset(64)
        //            make.left.right.equalTo(self.view)
        //            make.height.equalTo(40)
        //        }
        
        //        collectionView.snp_makeConstraints { (make) in
        //            make.left.right.equalTo(self.view)
        //            make.top.equalTo(ChooseToolBar.snp_bottom)
        //            make.height.equalTo(ScreenHeight - 148)
        //        }
        
        //        ChooseToolBar.selectedIndexCallBack = { (index: Int) -> () in
        //            // 滚动collectionView
        //            let indexPath = NSIndexPath(forItem: index, inSection: 0)
        //            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: true)
        //            self.collectionView.reloadData()
        //        }
        
        
    }
    
    //通知方法
    @objc private func pushForgetPWDController(){
        view.endEditing(true)
        self.navigationController?.pushViewController(BEForgetPwdVcController(), animated: true)
    }
    
    @objc private func dismissLoginController(){
        view.endEditing(true)
        dismiss(animated: true, completion: nil)
    }
    
    
    //移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    // 显示表情视图
    private lazy var collectionView: UICollectionView = {
        // 流水布局
        let layout = UICollectionViewFlowLayout()
        // 设置布局方向
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //        layout.itemSize = CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 148)
        layout.itemSize = CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        cv.dataSource = self
        // 设置代理
        cv.delegate = self
        // 注册cell
        cv.register(BELoginCell.self, forCellWithReuseIdentifier: "loginCell")
        cv.register(BESignupCell.self, forCellWithReuseIdentifier: "signupCell")
        
        // 设置分页滚动
        cv.isPagingEnabled = true
        //
        cv.bounces = false
        cv.backgroundColor = UIColor(white: 0.95, alpha: 1)
        return cv
    }()
}

extension BELoginAndSignUpController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    // 两个item
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        //        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KKHotRankingCellID, forIndexPath: indexPath)
        switch indexPath.item {
        case 0:
            let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: "loginCell", for: indexPath as IndexPath) as! BELoginCell
            return loginCell
        case 1:
            let signupCell = collectionView.dequeueReusableCell(withReuseIdentifier: "signupCell", for: indexPath as IndexPath) as! BESignupCell
            return signupCell
        default:
            break
        }
        return UICollectionViewCell()
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        view.endEditing(true)
    }
    
    //    func scrollViewDidScroll(scrollView: UIScrollView) {
    //
    //        //获取collectionView的 '中心点' 所在的的位置
    //        var center =  collectionView.center
    //        center.x = center.x + collectionView.contentOffset.x
    //        //判断当前屏幕显示的两个cell的frame 谁包含了这个中心点
    //        let indexPaths = collectionView.indexPathsForVisibleItems()
    //        for indexPath in indexPaths {
    //            let cell = collectionView.cellForItemAtIndexPath(indexPath)
    //            //判断frame是否包含了 '中心点'
    //            if cell!.frame.contains(center) {
    //                //可以设置toolbar选中的按钮
    //                ChooseToolBar.setSelectedBtn(indexPath.item)
    //            }
    //        }
    //    }
    
    
}

//loginCell
class BELoginCell: UICollectionViewCell{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let loginView = BELoginView.loginView() as! BELoginView
        contentView.addSubview(loginView)
        
        loginView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(contentView)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//signupView
class BESignupCell: UICollectionViewCell{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(BESignUpView.signupView())
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /*
     let urlString = "https://baidu.com"
     let url = NSURL(string: urlString)!
     let request = NSURLRequest(URL: url)
     LegalProvisionsWebView.loadRequest(request)
     */
    
}
